package com.example.amoxicilin.null_test.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Status;
import com.example.amoxicilin.null_test.domain.User;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends AppCompatActivity{

    /**
     * Id to identity READ_CONTACTS permission request.
     */
    private static final int REQUEST_READ_CONTACTS = 0;

    /**
     * A dummy authentication store containing known user names and passwords.
     * TODO: remove after connecting to a real authentication system.
     */
    private static final String[] DUMMY_CREDENTIALS = new String[]{
            "foo@example.com:hello", "bar@example.com:world"
    };
    /**
     * Keep track of the login task to ensure we can cancel it if requested.
     */

    // UI references.
    private AutoCompleteTextView mEmailView;
    private EditText mPasswordView;
    private String content1="";
    private String content2="";
    private ProgressBar progressBar;
    int usernumber;
    String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        mPasswordView = (EditText) findViewById(R.id.password);
        progressBar=(ProgressBar)findViewById(R.id.login_progress);


        mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_NULL) {
                    return true;
                }
                return false;
            }
        });
        Button mBrowserButton = (Button) findViewById(R.id.email_browser_button);
        mBrowserButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,MainActivity.class);
                Bundle bundle =new Bundle();
                bundle.putString("name","未登录");
                bundle.putInt("loginnumber",12345);
                intent.putExtras(bundle);
                startActivity(intent);

                finish();
            }
        });

        Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                content1 = mEmailView.getText().toString();
                content2 = mPasswordView.getText().toString();
                progressBar.setVisibility(view.VISIBLE);
                String path = "http://192.168.12.221:8080/tmoocserver/LoginServlet?name=" + content1 + "&password=" + content2;

                final String jsonString = HttpUtils.getJsonContent(path);
//                Toast.makeText(LoginActivity.this,jsonString+"",Toast.LENGTH_LONG).show();
                Status status = JsonTools.getStatus(null,jsonString);
//                Toast.makeText(LoginActivity.this,status.getStatusId()+"",Toast.LENGTH_LONG).show();
                User loginuser=JsonTools.getUser(null,jsonString);
                usernumber=loginuser.getNumber();
                username=loginuser.getName();



                if(1==status.getStatusId()){
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("系统消息")
                            .setMessage("登陆成功！")
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }})
                            .show();
                    Intent intent=new Intent();
                    intent.setClass(LoginActivity.this,MainActivity.class);

                    Bundle bundle =new Bundle();
                    bundle.putString("name",content1);
                    bundle.putInt("loginnumber",usernumber);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                }
                else
                {
                    new AlertDialog.Builder(LoginActivity.this)
                            .setTitle("系统消息")
                            .setMessage("密码错误！")
                            .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {}})
                            .show();
                }

            }
        });

    }
}

